<style scoped>
  .register{
    height:100%;
    background: #edf1f5;
  }
  input{
    border:none;
  }
  .header{
    height:60px;
    padding-top: 10px;
    display: flex;
    justify-content: center;
    align-items:center;
    border-bottom: 1px solid #E9E9E9;
    box-sizing: border-box;
    background: #ffffff;
  }
  .head-left{
    position: absolute;
    width:40px;
    height:60px;
    left:0;
    top:0;
    color: #7f7a8e;
    text-align: center;
  }
  .head-left h1{
    margin-top: 22px;
  }
  .head-middle{
    font-size: 16px;
  }
  .head-right{
    position: absolute;
    right:10px;
    top:15px;
  }
  .content{
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 100%;
    margin-top: 80px;
    /*height:100%;*/
  }
  .content>h3{
    margin-bottom: 20px;
  }
  .input{
    width:100%;
  }
  .input>div{
    margin:0 5%;
  }
  .input>div>.zhmm{
    background: #ffffff;
    border-radius: 10px;
  }
  .input>div>.zhmm>div{
    display: flex;
    align-items: center;
  }
  .input>div>.zhmm>div>label{
    flex: 2;
    display: flex;
    justify-content: center;
    align-items:center;
  }
  .input>div>.zhmm>div>input{
    flex: 7;
    display: flex;
    align-items: center;
    height:80%;
  }
  .input>div>.zhmm>div{
    border-bottom: 1px solid #e9e9e9;
    box-sizing: border-box;
  }
  .input>div>.zhmm>.yanzhengma label{
    /*display: flex;*/
    flex:2;
    /*flex-direction: row;*/
  }
  .input>div>.zhmm>.yanzhengma div{
    flex: 7;
    display: flex;
    height:100%;
    flex-direction: row;
  }
  .input>div>.zhmm>.yanzhengma div input{
    flex: 1;
  }
  .input>div>.zhmm>.yanzhengma div p{
    display: flex;
    justify-content: center;
    align-items: center;
    height:100%;
    background: #4D66FF;
    color: #ffffff;
    border-radius: 4px;
  }
  .submit{
    margin-top: 30px;
    border-radius: 10px;
    background: #4D66FF;
  }
  .submit a{
    height:100%;
    width:100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .submit h3{
    color: #ffffff;
  }
  @media screen and (max-width: 320px) {
    .input>div>.zhmm>div{
      height:43px;
    }
    .submit{
      height:43px;
    }
    .input>div>.zhmm input{
      font-size: 1.2rem;
    }
  }
  @media screen and (max-width:375px) and (min-width:321px) {
    .input>div>.zhmm>div{
      height:50px;
    }
    .submit{
      height:50px;
    }
    .input>div>.zhmm input{
      font-size: 1.4rem;
    }
  }
  @media screen and (min-width: 376px){
    .input>div>.zhmm>div{
      height:60px;
    }
    .submit{
      height:60px;
    }
    .input>div>.zhmm input{
      font-size: 1.6rem;
    }
  }
  .confirm{
    display: flex;
    justify-content: center;
    margin-top: 8px;
  }

  /*验证*/
  .test{
    margin-left: 20px;
    margin-top: 4px;
    color: #e43937;
  }

  .name{
    position: relative;
  }
  .name>p{
    position: absolute;
    top:14px;
    right:10px;
  }
</style>

<template>
  <div class="register" ref="nana">
    <div class="header">
      <div class="head-left" @click="goBack"><h1 class="iconfont icon-xiangzuo1"></h1></div>
      <div class="head-middle"><h3>重置密码</h3></div>
    </div>
    <form>
<div class="content">
      <h3>重置登录密码</h3>
      <div class="input">
        <div>
          <div class="zhmm">
            <div class="name">
              <label><p>账户</p></label>
              <input type="tel" placeholder="请输入手机号" v-model="account">
              <p class="iconfont icon-guanbi" @click="clearPhone"></p>
            </div>
            <div class="yanzhengma">
              <label><p>验证码</p></label>
              <div>
                <input @keyup.enter="next" type="number" placeholder="请输入登录验证码" v-model="verificationCode">
                <p @click="send">
                  <span v-if="sendMsgDisabled">{{time+'秒后获取'}}</span>
                  <span v-if="!sendMsgDisabled">{{sendCode}}</span>
                </p>
              </div>
            </div>
            <small class="test" v-if="hasCode">{{testCode}}</small>
          </div>
          <div class="submit" @click="next">
            <router-link to=""><h3>下一步</h3></router-link>
          </div>
        </div>
      </div>
    </div>
    </form>
  </div>
</template>

<script>
  import axios from 'axios'
  import { MessageBox } from 'mint-ui'
  import host from '../config.js'
  import { Toast } from 'mint-ui'

  export default {
    data () {
      return {
        verificationCode: null,
        account: null,
        message: null,
        sendCode: '发送验证码',
        time: 60, // 发送验证码倒计时
        sendMsgDisabled: false,
//        验证验证码
        hasCode: false,
        tesCode: null,
        toNewpwd: null
      }
    },
    watch: {
      verificationCode: function (newValue, oldValue) {
        if (newValue !== ''){
          this.hasCode = false
        }
      }
    },
    mounted () {
      let h = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
      let h1 = h
      this.$refs.nana.style.height = h1 + 'px'
    },
    methods: {
      goBack () {
        this.$router.go(-1)
      },
      send () {
        if(this.account === null || this.account === ''){
          Toast({
            message: '账户不能为空',
            duration: 1500
          })
          return
        }
        if(this.account.trim().length === 0) {
          Toast({
            message: '账户不能为空',
            duration: 1500
          })
          return
        }
        if(!(/^1[3|4|5|7|8][0-9]\d{4,8}$/.test(this.account))){
          Toast({
            'message': '账户不正确',
            duration: 1500
          })
          return;
        }
        if(this.sendMsgDisabled === true) {
          return
        }
        let that = this
        let sendData = {
          "phone": that.account,
          "act": "forget"
        }
        function sedMessage () {
          that.baseService.post('/usr/verifyCode', sendData).then(function (data) {
            console.log(data.data)
            if(data.data.code != 200){
              return
            }else{
//              let message = data.data.data.code
            }
          })
        }
        if(that.sendMsgDisabled === false){
          sedMessage ()
        }

        that.sendMsgDisabled = true
        let interval = window.setInterval(function() {
          if ((that.time--) <= 0) {
            that.time = 60
            that.sendMsgDisabled = false
            window.clearInterval(interval)
          }
        }, 1000)
      },
      next () {
        if(this.verificationCode === null){
          this.hasCode = true
          this.testCode = '验证码不能为空'
          return
        }

        let that = this
        axios({
          method: 'post',
          url: host.host + "/api/usr/verifyCodeCheck",
          data: JSON.stringify({
            "phone": that.account,
            "act": "forget",
            "code": that.verificationCode
          })
        }).then(function(data) {
          if(data.data.code === 200){
            that.toNewpwd = {
              "phone": that.account,
              "message": that.verificationCode
            }
            that.$store.state.sendCode = that.toNewpwd
            that.$router.push({'path':'/newpwd'})
          }else{
            Toast({
              message: data.data.msg,
              duration: 1000
            })
          }
        })
      },
      clearPhone () {
        this.account = null
      }
    }
  }
</script>
